package com.sparrow.manage.article;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface ArticleDao extends JpaRepository<Article, String> {

    @Modifying
    @Transactional
    @Query(value = "update tb_article set carousel = ?1 where ID_ = ?2", nativeQuery = true)
    void updateCarouselById(Integer carousel, String id);

    @Query(value = "select * from tb_article where type = ?1 order by create_time is null, create_time desc, priority is null, priority desc limit ?2", nativeQuery = true)
    List<Article> queryByType(String type, int size);

    @Query(value = "select * from tb_article where carousel = 1 and exists (select 1 from tb_attachment where tb_attachment.relate_id = tb_article.id_) order by create_time is null, create_time desc, priority is null, priority desc limit ?1", nativeQuery = true)
    List<Article> queryCarousel(int size);
}
